//
//  CategoryCollectionViewCell.m
//  openEye
//
//  Created by 徐悟源 on 16/3/31.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "CategoryCollectionViewCell.h"
#import "Categories.h"
#import "UIImageView+WebCache.h"

@interface CategoryCollectionViewCell()

/** 背景大图 */
@property (nonatomic, weak) UIImageView *bgView;

/** 蒙板 */
@property (nonatomic, weak) UIView *coverView;

/** 中间标题 */
@property (nonatomic, weak) UILabel *titleLabel;

@end

@implementation CategoryCollectionViewCell

/** 
 * 初始化子控件
 */
- (void)setupCategoriesCell
{
    // 背景大图
    UIImageView *bgView = [[UIImageView alloc ] init];
    _bgView = bgView;
    _bgView.contentMode = UIViewContentModeScaleAspectFill;
    
    _bgView.clipsToBounds = YES;
    _bgView.layer.cornerRadius = 5.0;
    
    [self addSubview:bgView];
    
    
    /** 蒙板 */
    UIView *coverView = [[UIView alloc] init];
    _coverView = coverView;
    _coverView.backgroundColor = coverViewColor;
    [self.bgView addSubview:coverView];
    
    /** 中间标题 */
    UILabel *titleLabel = [[UILabel alloc] init];
    _titleLabel = titleLabel;
    _titleLabel.font = Font_ChinaBold(18);
    _titleLabel.textColor = [UIColor whiteColor];
    [self.coverView addSubview:titleLabel];
}

/** 初始化cell */
- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupCategoriesCell];
    }
    return self;
}

/** 子控件布局 */
- (void)layoutSubviews
{
    // 背景大图
    CGFloat bgViewW = self.contentView.my_Width;
    CGFloat bgViewH = bgViewW;
    _bgView.my_Width = bgViewW;
    _bgView.my_Height = bgViewH;
    
    // 蒙板
    _coverView.frame = _bgView.frame;
    
    // 中间标题
    CGFloat titleLabelW = [[self formatterStr:_category.name] sizeWithFont:_titleLabel.font].width;
    CGFloat titleLabelH = [[self formatterStr:_category.name] sizeWithFont:_titleLabel.font].height;
    
    CGFloat titleLabelX = (self.bgView.my_Width - titleLabelW) / 2;
    CGFloat titleLabelY = (self.bgView.my_Height - titleLabelH) / 2;
    
    _titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
}

- (void)setCategory:(Categorie *)category
{
    _category = category;
    
    NSURL *url = [NSURL URLWithString:category.bgPicture];
    [self.bgView sd_setImageWithURL:url];
    
    self.titleLabel.text = [self formatterStr:category.name];
}

/** 格式工具 */
- (NSString *)formatterStr:(NSString *)str
{
    return [NSString stringWithFormat:@"#%@",str];
}

@end
